// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const wxContext = cloud.getWXContext()
    var limit = 15;
    var alreadyHave = event.alreadyHave;

    const result = await db.collection('wrongNote').where({
      user_id: wxContext.OPENID,
    }).skip(alreadyHave).limit(limit).get();

    if (result.data[0] == null) {
      return {
        _log: 1,
        words: new Array(),
        num: new Array(),
      }
    } else {
      var words = new Array();
      var num = new Array();
      for (let i = 0; i < result.data.length; i++) {
        var word = await db.collection('word').where({
          word_id: result.data[i].word_id,
        }).get();
        words.push(word.data[0]);
        num.push(result.data[i].number);
      }
      return {
        _log: 2,
        words: words,
        num: num,
      }
    }
  } catch (e) {
    console.error(e);
  }
}